<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="app-container">
      <el-card class="search-card-box" shadow="never">
        <el-form
          ref="carModelsFormData"
          :model="carModelsFormData"
          label-width="80px"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="车型编号:"
                style="margin-bottom: 0"
                label-width="110px"
              >
                <el-input
                  v-model="carModelsFormData.id"
                  placeholder="请输入车型编号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="应载重量:"
                style="margin-bottom: 0"
                label-width="110px"
              >
                <el-input
                  v-model="carModelsFormData.allowableLoad"
                  placeholder="请输入应载重量"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="应载体积:"
                style="margin-bottom: 0"
                label-width="110px"
              >
                <el-input
                  v-model="carModelsFormData.allowableVolume"
                  placeholder="请输入应载体积"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="车型类型:"
                style="margin-bottom: 0"
                label-width="110px"
              >
                <el-input
                  v-model="carModelsFormData.name"
                  placeholder="请输入车型类型"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8" style="padding-left:70px">
              <el-button
                type="warning"
                width="90px"
                height="40px"
                @click="handleFilter('查询')"
              >搜索
              </el-button
              >
              <el-button
                width="90px"
                height="40px"
                plain
                class="reset-btn"
                @click="resetForm('carModelsFormData')"
              >重置
              </el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card shadow="never" class="table-card-box">
          <el-button
            type="warning"
            style="margin-bottom:20px"
            @click="handleAdd()"
          >新增车型
          </el-button>
          <el-table
            ref="multipleTable"
            :data="dataList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!dataList || dataList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong@2x.png"
                alt="img"
                style="margin-top: 20px;width: 14%;height: 14%;"
              />
              <p
                style="
          margin-top: -20px;
          padding-bottom: 0px;
          "
              >
                这里空空如也
              </p>
            </div>
            <el-card
              v-show="
                (!dataList || dataList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column align="left" type="index" label="序号" width="60">
              <template slot-scope="scope">
                <span>{{
                    scope.$index +
                    (carModelsFormData.page - 1) *
                    carModelsFormData.pageSize +
                    1
                  }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="车型编号" min-width="200" prop="id">

            </el-table-column>
            <el-table-column align="left" label="车辆类型" min-width="110" prop="name">
            </el-table-column>
            <el-table-column align="left" label="车型数量" min-width="110" prop="num">
            </el-table-column>
            <el-table-column align="left" label="应载重量（吨）" min-width="130" prop="allowableLoad">
            </el-table-column>
            <el-table-column align="left" label="应载体积（立方米）" min-width="160" prop="allowableVolume">
            </el-table-column>
            <el-table-column align="left" label="长（米）" min-width="80" prop="measureLong">
            </el-table-column>
            <el-table-column align="left" label="宽（米）" min-width="80" property="measureWidth">
            </el-table-column>
            <el-table-column align="left" label="高（米）" min-width="100" prop="measureHigh">
            </el-table-column>
            <el-table-column
              align="center"
              fixed="right"
              label="操作"
              width="120"
              class-name="small-padding fixed-width"
            >
              <el-col width="100%" slot-scope="scope">
                <!-- 调用的编辑方法 -->
                <el-button
                  @click="handleAdd(scope.row)"
                  class="customer-add-btn"
                  style="color:#419EFF;border: none;padding: 0; margin: 0;background-color:transparent;"
                >编辑
                </el-button>
                <span class="line"></span>
                <!-- 调用运费的删除方法 -->
                <el-button
                  @click="handleDel(scope.row.id)"
                  class="customer-add-btn"
                  style="border: none;padding: 0; margin: 0;background-color:transparent;color:#F56C6C;"
                >删除
                </el-button>
              </el-col>
            </el-table-column>
          </el-table>
          <!-- end -->
          <!-- 分页 -->
          <!-- 分页 -->
          <div v-show="dataList && dataList.length > 0" class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="Number(carModelsFormData.page)"
                :total="Number(4)"
                :page-size="Number(carModelsFormData.pageSize)"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-card>
        <!-- 新增车型 -->
        <car-model-add
          ref="addCarModel"
          class="add-form-dialog"
          @resetForm="reset"
          :title-info="titleInfo"
          :form-base="formData"
          :agency-options="agencyOptions"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { carModeDelAPI, carModelListAPI } from '@/api/car-models'
import CarModelAdd from '@/views/transit/components/car-models-add.vue'

export default {
  components: { CarModelAdd },
  data() {
    return {
      listLoading: false,
      dataList: [],
      carModelsFormData: {
        page: 1,
        pageSize: 10,
        id: '',
        orderId: '' // 订单id
      },
      searchkey: false,
      titleInfo: {},
      formData: {},
      agencyOptions: [],
      list: [
        {
          id: '123456789',
          allowableLoad: '5',
          allowableVolume: '7',
          measureHigh: '3',
          measureLong: '6',
          measureWidth: '3',
          num: '2',
          name: '卡车'
        },
        {
          id: '123456789',
          allowableLoad: '6',
          allowableVolume: '9',
          measureHigh: '3',
          measureLong: '6',
          measureWidth: '3',
          num: '2',
          name: '卡车'
        },
        {
          id: '854456789',
          allowableLoad: '6',
          allowableVolume: '10',
          measureHigh: '6',
          measureLong: '8',
          measureWidth: '3',
          num: '1',
          name: '轿车'
        },
        {
          id: '567456789',
          allowableLoad: '6',
          allowableVolume: '10',
          measureHigh: '3',
          measureLong: '7',
          measureWidth: '3',
          num: '4',
          name: '轿车'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 删除车型
    handleDel(id) {
      this.$confirm('是否删除该车型?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // await carModeDelAPI(id)
        this.$message.warning('很抱歉,演示系统,不允许删除!')
        await this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 重置
    reset() {
      this.getList()
    },
    // 获取列表数据
    async getList() {
      this.listLoading = true
      const { data: items } = await carModelListAPI()
      this.listLoading = false
      // 获取数据给dataList
      this.dataList = items
      // 如果响应吗不是200，则用自己编写的数据
      if (!this.dataList) {
        this.dataList = this.list
      }
    },
    // 重置
    resetForm(carModelsFormData) {
      // 之所以注释掉是因为这里必须配合prop对表单进行赋值，但后端有的字段不支持空串
      // this.$refs[carModelsFormData].resetFields()
      this.carModelsFormData = {
        page: 1,
        pageSize: 10,
        status: this.carModelsFormData.status
      }
      this.searchkey = false
      this.getList()
    },
    // 搜索信息
    handleFilter() {
      this.carModelsFormData.page = 1
      this.getList(this.carModelsFormData)
      this.searchkey = true
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.carModelsFormData.pageSize = val
      if (this.carModelsFormData.page === 1) {
        this.getList(this.carModelsFormData)
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.waybillSearchFormData.page = val
      this.getList()
    },
    // 新增+编辑车型
    async handleAdd(row) {
      if (row) {
        this.formData.name = row.name
        this.formData.allowableLoad = row.allowableLoad
        this.formData.allowableVolume = row.allowableVolume
        this.formData.measureLong = row.measureLong
        this.formData.measureWidth = row.measureWidth
        this.formData.measureHigh = row.measureHigh
        this.formData.id = row.id
        console.log(this.formData)
        this.titleInfo.text = '编辑车型'
        this.$refs.addCarModel.dialogFormV()
      } else {
        this.titleInfo.text = '新增车型'
        this.formData = {}
        this.$refs.addCarModel.dialogFormV()
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.add-btn {
  margin-bottom: 20px;
}

.alert {
  margin: 10px 0px;
}

.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.car-models {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }

  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }

  .warn-img {
    width: 40px;
  }

  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

.el-form {
  ::v-deep .el-col {
    margin: 10px 0;
  }
}

.reset-btn {
  height: 40px !important;
}

.line {
  display: inline-block;
  width: 1px;
  height: 12px;
  background: #E4E7ED;
  margin: 0 10px;
}
</style>
